package Linked_list;

import java.util.ArrayList;
import java.util.List;

public class Listoperation {
    public static Node create_list(){
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        n1.next = n2;
        n2.next = n3;
        n3.next = null;
        return  n1;
    }

    public static void bianli_print(Node head){
        Node cur = head;
        while (cur!=null){
            System.out.println(cur.val);
            cur = cur.next;
        }
    }

    public static List<Node>查找链表中和某元素相等的值(Node head,int tag){
    List<Node> ans = new ArrayList<>();
        for(Node cur = head;cur!= null;cur = cur.next){
            if(cur.val == tag){
                ans.add(cur);
            }
        }
        return ans;
    }
    public static Node find_the_third(Node head){
        Node cur = head;
        cur = cur.next;
        cur = cur.next;
        return cur;
    }

    public static Node find第n个节点(Node head,int n){
        Node cur = head;
        for (int i = 0; i <n-1 ; i++) {
            cur = cur.next;
        }
        return cur;
    }

    public static Node find倒数第1个(Node head){
        Node cur = head;
        while (cur.next!=null){
            cur = cur.next;
        }
        return cur;
    }


    public static Node find倒数第2个(Node head){
        Node cur = head;
        while (cur.next.next!=null){
            cur = cur.next;
        }
        return cur;
    }


    public static Node head_add(Node head,int e){
        Node node = new Node(e);
            node.next = head;
        return node;
    }

    public static Node createList() {
        return new Node(0);
    }

    public static Node headAdd(Node head, int ele) {
        if (head == null) {
            head = new Node(0);
        }
        Node node = new Node(ele);
        node.next = head.next;
        head.next = node;

        return node;
    }

    public static Node head_delete(Node head){
        if(head==null){
            throw new RuntimeException("链表是空的");
        }
        head = head.next ;
        return head;
    }

    public static void printList(Node head) {
        System.out.println(head.val);
        while ((head=head.next) != null) {
            System.out.println(head.val);
        }
    }

    public static Node weiadd(Node head,int e){
        Node node = new Node(e);
        if(head == null){
            return node;
        }
        Node cur = head;
        while (cur.next!=null){
            cur = cur.next;
        }
        cur.next = node;
        return head;
    }

    public static Node weidelete(Node head){
        if(head == null){
            throw new RuntimeException();
        }
        if(head.next==null){
            return head;
        }
        Node  cur = head;
        while(cur.next.next!=null){
            cur = cur.next;
        }
        cur.next = null;
        return head;
    }
    public static void insert_node(Node node,int e){
        Node newnode  = new Node(e);
        newnode.next = node.next;
        node.next = newnode;
    }
//删除指定结点之后的结点
    public static void delete_node(Node node){
        node.next = node.next.next;
    }
    public static void main(String[] args) {
        Node head = null;
//        head = head_add(head, 100);
//        head = head_add(head, 200);
//        head = head_add(head, 300);
//        head = head_delete(head);
//        head = weiadd(head,10);
//        head = weidelete(head);
        head = create_list();
        bianli_print(head);
//        Node  node = find第n个节点(head,2);
//        insert_node(node,5);
//        bianli_print(head);
//        delete_node(node);
//        bianli_print(head);
//        bianli_print(head);
//        find_the_third(head);
//        head_delete(head);
//        headAdd(head,100);
//        headAdd(head,200);
//        headAdd(head,300);
//        headAdd(head,400);
//        headAdd(head,500);
      printList(head);
    }
}